<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>查询配置管理</title>
    <%@include file="../../sys_common_head.jsp"%>
</head>
<body class="pear-container">


<!-- 查询条件 -->


<div class="layui-card">
    <div class="layui-card-body">
        <table id="data-table" lay-filter="data-table"></table>
    </div>
</div>
</body>

<!-- 工具栏 -->
<script type="text/html" id="data-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<!-- 每行操作栏 -->
<script type="text/html" id="data-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>


<script>
    layui.use(['table', 'form', 'jquery','common'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let MODULE_PATH = "<%=request.getContextPath()%>/student/sqlquery/";

        let cols = [
            [
                {type: 'checkbox'},
                {field: 'id', title: ''},
                {field: 'colConfig', title: '配置列显示'},
                {field: 'sqlVar', title: '查询的sql'},
                {title: '操作', fixed: 'right', width: 165, align:'center', toolbar: '#data-bar'}
            ]
        ]

        table.render({
            elem: '#data-table',
            url: MODULE_PATH + 'list',
            page: true,
            method:'post',
            cols: cols,
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            toolbar: '#data-toolbar'
        });

        table.on('tool(data-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(data-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(data-query)', function (data) {
            table.reload('data-table', {where: data.field})
            return false;
        });

        window.add = function() {
            layer.open({
                type: 2,
                title: '新增数据',
                shade: 0.1,
                area: [common.isModile()?'100%':'500px', common.isModile()?'100%':'400px'],
                content: MODULE_PATH + 'add'
            });
        }

        window.edit = function(obj) {
            layer.open({
                type: 2,
                title: '修改数据',
                shade: 0.1,
                area: ['500px', '400px'],
                content: MODULE_PATH + 'edit?id=' + obj.data['id']
            });
        }

        window.remove = function(obj) {
            layer.confirm('确定要删除该数据？', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function(result) {
                        layer.close(loading);
                        if (result.code == 0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function(obj) {

            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }

            layer.confirm('确定要删除这些数据？', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "removeBatch/" + ids,
                    dataType: 'json',
                    type: 'delete',
                    success: function(result) {
                        layer.close(loading);
                        if (result.code == 0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                table.reload('data-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }


        window.refresh = function () {
            table.reload('data-table');
        }
    })
</script>
</html>